from django.db import models

class K8sConfig(models.Model):
    """k8s_api连接配置文件"""
    config_content = models.TextField(blank=True, null=False, verbose_name="配置内容")
    project = models.ForeignKey("user_system.Project", on_delete=models.CASCADE, verbose_name="属于哪个项目")
    user = models.ForeignKey("user_system.User", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="属于哪个用户")

    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.config_content

    class Meta:
        verbose_name_plural = "k8s_api连接配置文件"
        db_table = "k8sconfig"

class K8sTemplateYaml(models.Model):
    """k8s 模板 yaml文件"""
    name = models.CharField(max_length=128, blank=True, null=False, verbose_name="模板名称")
    content = models.TextField(blank=True, null=False, verbose_name="模板内容")
    project = models.ForeignKey("user_system.Project", on_delete=models.CASCADE, verbose_name="属于哪个项目")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "k8s 模板 yaml文件"
        db_table = "K8stemplateyaml"

class K8sYaml(models.Model):
    """k8s yaml文件"""
    server_id = models.IntegerField(blank=True, null=False, verbose_name="server_id")
    yaml_name = models.CharField(max_length=128, blank=True, null=False, verbose_name="yaml文件名")
    namespace = models.CharField(max_length=64, blank=True, null=False, verbose_name="namespace")
    deployment_name = models.CharField(max_length=64, blank=True, null=False, verbose_name="deployment_name")
    replicas = models.IntegerField(default=1,verbose_name="副本数")
    nodePort_list = models.CharField(max_length=128, blank=True, null=False, verbose_name="nodePort_list")
    yaml_content = models.TextField(blank=True, null=False, verbose_name="yaml内容")
    version = models.CharField(max_length=128, blank=True, null=True, verbose_name="镜像版本")
    server_name = models.CharField(max_length=128, blank=True, null=False, verbose_name="服务器名称")
    project = models.ForeignKey("user_system.Project", on_delete=models.CASCADE, verbose_name="属于哪个项目")
    group = models.ForeignKey("user_system.GameServerGroup", on_delete=models.CASCADE, verbose_name="属于哪个分区")
    template = models.ForeignKey("K8sTemplateYaml", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="所属模板")
    deploy_status_choices = (
        (1, "未部署"),
        (2, "已部署"),
        (3, "已停止"),
    )
    deploy_status = models.SmallIntegerField(choices=deploy_status_choices, default=1, verbose_name="部署状态")
    pod_ip = models.CharField(max_length=128, blank=True, null=True, verbose_name="Pod地址")
    status = models.CharField(max_length=128, blank=True, null=True, verbose_name="运行状态")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.yaml_name

    class Meta:
        verbose_name_plural = "k8s yaml文件"
        db_table = "k8syaml"

class TaskList(models.Model):
    """任务表"""
    uuid_str = models.CharField(max_length=128,blank=True,null=True,verbose_name="唯一标识符")
    name = models.CharField(max_length=128,blank=True,null=True,verbose_name="任务名称")
    op_task = models.TextField(blank=True,null=True,verbose_name="执行内容")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    status_choices = (
        (1, "执行中"),
        (2, "执行成功"),
        (3, "执行失败"),
        (4, "未开始"),
    )
    status = models.SmallIntegerField(choices=status_choices,default=1,verbose_name="执行状态")
    done_time = models.DateTimeField(auto_now=True, verbose_name="完成时间")
    user = models.ForeignKey("user_system.User", null=True, blank=True, on_delete=models.SET_NULL,verbose_name="属于哪个用户")
    project = models.ForeignKey("user_system.Project",on_delete=models.CASCADE,verbose_name="属于哪个项目")
    success_result = models.CharField(max_length=512,blank=True,null=True,default=None,verbose_name="成功的结果")
    fail_result = models.CharField(max_length=512, blank=True, null=True,default=None,verbose_name="失败的结果")
    exception_message = models.TextField(blank=True,null=True,default=None,verbose_name="异常信息")
    script_type = models.CharField(max_length=128,blank=True,null=True,verbose_name="执行类型")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "任务表"
        db_table = "tasklist"
